package com.onlyxiahui.common.lib.util.http;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.protocol.HTTP;

/**
 * 
 * Date 2019-01-06 18:39:35<br>
 * Description 基于 httpclient4版本的http工具类
 * 
 * @author XiaHui<br>
 * @since 1.0.0
 */
public class HttpClient4Util {

	public static final HttpClientAdaptor HTTP_CLIENT = new HttpClientAdaptor();

	/******************* Get *********************/

	public static String doGet(String url) {
		return doGet(url, Collections.emptyMap(), Collections.emptyMap(), null, null);
	}

	public static String doGet(String url, Map<String, Object> params) {
		return doGet(url, Collections.emptyMap(), params, null, null);
	}

	public static String doGet(String url, Map<String, String> headers, Map<String, Object> params) {
		return doGet(url, headers, params, null, null);
	}

	public static String doGet(String url, Map<String, String> headers, Map<String, Object> params, String requestCharset) {
		return doGet(url, headers, params, requestCharset, null);
	}

	public static String doGet(
			String url,
			Map<String, String> headers,
			Map<String, Object> params,
			String requestCharset,
			String responseCharset) {
		return HTTP_CLIENT.doGet(url, headers, params, requestCharset, responseCharset);
	}

	/******************* Post body *********************/

	public static String doPost(String url, String body) {
		return doPost(url, Collections.emptyMap(), body, null, null);
	}

	public static String doPost(String url, Map<String, String> headers, String body) {
		return doPost(url, headers, body, null, null);
	}

	public static String doPost(String url, Map<String, String> headers, String body, String requestCharset) {
		return doPost(url, headers, body, requestCharset, null);
	}

	public static String doPost(
			String url,
			Map<String, String> headers,
			String body,
			String requestCharset,
			String responseCharset) {
		return HTTP_CLIENT.doPost(url, headers, body, requestCharset);
	}

	/******************* Post params *********************/
	public static String doPost(String url) {
		return doPost(url, Collections.emptyMap(), Collections.emptyMap(), null, null);
	}

	public static String doPost(String url, Map<String, Object> params) {
		return doPost(url, Collections.emptyMap(), params, null, null);
	}

	public static String doPost(String url, Map<String, String> headers, Map<String, Object> params) {
		return doPost(url, headers, params, null, null);
	}

	public static String doPost(String url, Map<String, String> headers, Map<String, Object> params, String requestCharset) {
		return doPost(url, headers, params, requestCharset, null);
	}

	public static String doPost(String url,
			Map<String, String> headers,
			Map<String, Object> params,
			String requestCharset,
			String responseCharset) {
		return HTTP_CLIENT.doPost(url, headers, params, requestCharset, responseCharset);
	}

	/*********/

	public static String jsonPost(String url, String body) {
		return jsonPost(url, null, body);
	}

	public static String jsonPost(String url, Map<String, String> headers, String body) {
		Map<String, String> map = null == headers ? new HashMap<>() : headers;
		map.put(HTTP.CONTENT_TYPE, "application/json; charset=UTF-8");
		map.put("Accept", "application/json; charset=UTF-8");
		return doPost(url, map, body, null, null);
	}
}
